home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / 3DTOSHI2.ZIP / mpg3d / source / G3dmatrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-21  |  6.3 KB  |  237 lines

  1.  
  2. // g3dmatrl.cpp
  3. //
  4. // Copyright (c) 1996 by Toshiaki Tsuji, all rights reserved.
  5.  
  6. #include "stdgfx.h"
  7. #include "g3dmatrl.h"
  8.  
  9. //******************************************
  10. //
  11. // G3DMATERIAL Class
  12. //
  13. //******************************************
  14.  
  15. G3DMATERIAL::G3DMATERIAL ()
  16.   {
  17.     Texture = NULL;
  18.     Ambient = 0;
  19.     Transparency = 0;
  20.     Attributes = 0;
  21.     Name[0] = 0;
  22.     ID = 0;
  23.   } // End of Constructor for G3DMATERIAL 
  24.  
  25. G3DMATERIAL::~G3DMATERIAL ()
  26.   {
  27.   } // End of Destructor for G3DMATERIAL 
  28.   
  29. VOID G3DMATERIAL::SetTexture ( ANIMIMAGE *Image )
  30.   {
  31.     Texture = Image;
  32.   } // End of SetTexture for G3DMATERIAL 
  33.  
  34. VOID G3DMATERIAL::SetName ( STRING NewName )
  35.   {
  36.     strcpy ( Name, NewName );  
  37.   } // End of SetName for G3DMATERIAL
  38.   
  39. VOID G3DMATERIAL::SetID ( LONG NewID )
  40.   {
  41.     ID = NewID;  
  42.   } // End of SetID for G3DMATERIAL
  43.   
  44.  
  45. //******************************************
  46. //
  47. // G3DMATERIAL Class
  48. //
  49. //******************************************
  50.  
  51. G3DMATERIALLIB::G3DMATERIALLIB ()
  52.   {
  53.     Materials = NULL;
  54.     NumMaterials = 0;
  55.     Textures = NULL;
  56.     NumTextures = 0;
  57.   } // End of Constructor for G3DMATERIALLIB
  58.  
  59. G3DMATERIALLIB::~G3DMATERIALLIB ()
  60.   {
  61.     DestroyMaterials ();
  62.     DestroyTextures ();
  63.   } // End of Destructor for G3DMATERIALLIB
  64.       
  65. VOID G3DMATERIALLIB::CreateMaterials ( LONG Num )
  66.   {
  67.     DestroyMaterials ();
  68.     
  69.     Materials = new G3DMATERIAL* [Num];
  70.     if (Materials==NULL)
  71.       return;
  72.       
  73.     LONG i;
  74.     
  75.     NumMaterials = Num;
  76.     for (i=0;i<NumMaterials;i++)
  77.       {
  78.         Materials[i] = new G3DMATERIAL ();
  79.       } // End for  
  80.   } // End of CreateMaterials for G3DMATERIALLIB
  81.  
  82. VOID G3DMATERIALLIB::DestroyMaterials ()
  83.   {
  84.     LONG i;
  85.     if (Materials!=NULL)
  86.       {
  87.         for (i=0;i<NumMaterials;i++)
  88.           {
  89.             if (Materials[i]!=NULL)
  90.               delete Materials[i];
  91.           } // End for
  92.         delete Materials;
  93.       } // End if    
  94.     NumMaterials = 0;
  95.     Materials = NULL;        
  96.   } // End of DestroyMaterials for G3DMATERIALLIB
  97.  
  98. VOID G3DMATERIALLIB::CreateTextures ( LONG Num )
  99.   {
  100.     DestroyTextures ();
  101.     
  102.     Textures = new ANIMIMAGE* [Num];
  103.     if (Textures==NULL)
  104.       return;
  105.       
  106.     LONG i;
  107.     
  108.     NumTextures = Num;
  109.     for (i=0;i<NumTextures;i++)
  110.       {
  111.         Textures[i] = new ANIMIMAGE ();
  112.       } // End for  
  113.   } // End of CreateTextures for G3DMATERIALLIB
  114.  
  115. VOID G3DMATERIALLIB::DestroyTextures ()
  116.   {
  117.     LONG i;
  118.     if (Textures!=NULL)
  119.       {
  120.         for (i=0;i<NumTextures;i++)
  121.           {
  122.             if (Textures[i]!=NULL)
  123.               delete Textures[i];
  124.           } // End for
  125.         delete Textures;
  126.       } // End if    
  127.     NumTextures = 0;
  128.     Textures = NULL;        
  129.   } // End of DestroyTextures for G3DMATERIALLIB
  130.  
  131. G3DMATERIAL* G3DMATERIALLIB::FindMaterialByName ( STRING SearchName )
  132.   {
  133.     LONG i;
  134.     
  135.     for (i=0;i<NumMaterials;i++)
  136.       {
  137.         if (strcmp(SearchName,Materials[i]->GetName())==0)
  138.           return Materials[i];           
  139.       } // End for
  140.     return NULL;  
  141.   } // End of FindMaterialByName for G3DMATERIALLIB
  142.  
  143. G3DMATERIAL* G3DMATERIALLIB::FindMaterialByID ( LONG SearchID )
  144.   {
  145.     LONG i;
  146.     
  147.     for (i=0;i<NumMaterials;i++)
  148.       {
  149.         if (SearchID==Materials[i]->GetID())
  150.           return Materials[i];           
  151.       } // End for
  152.     return NULL;  
  153.   } // End of FindMaterialByID for G3DMATERIALLIB
  154.  
  155. BOOLEAN G3DMATERIALLIB::LoadTexture ( LONG Index, STRING FileName, BOOLEAN FileType,
  156.                                       RGBPALETTE *Pal )
  157.   {
  158.     if ((Index<0)||(Index>=NumTextures))
  159.       return FAILURE;
  160.  
  161.     BOOLEAN Result = FAILURE;
  162.  
  163.     ANIMIMAGE *Texture = GetTexture ( Index );
  164.     Texture->Create ( IMAGE_8BIT, 256, 256 );
  165.     
  166.     RGBPALETTE *LoadedPal = NULL;
  167.     COLORTABLE *MatchTable = NULL;
  168.     FLICFILE *Flic = NULL;
  169.     
  170.     switch (FileType)
  171.       {
  172.         case TEXTURE_STATIC :
  173.           LoadedPal = new RGBPALETTE ();
  174.           Result = Grafix.LoadImage ( FileName, Texture, LoadedPal );
  175.           break;  
  176.         case TEXTURE_FLIC :
  177.           Flic = new FLICFILE ();
  178.           Result = Flic->Load ( FileName );
  179.           Flic->GetFirstPalette ();
  180.           LoadedPal = Flic->GetPalette ();
  181.           break;  
  182.         case TEXTURE_ANIM :
  183.           break;  
  184.       } // End switch
  185.  
  186.     LONG i;
  187.     if (Result==SUCCESS)
  188.       {
  189.         if (Pal!=NULL)
  190.           {
  191.             if (Pal->IsIdentical(LoadedPal)==FALSE)
  192.               {
  193.                 if (FileType==TEXTURE_STATIC)
  194.                   {
  195.                     MatchTable= new COLORTABLE ();  
  196.                     MatchTable->CopyPalette ( Pal );  
  197.                     MatchTable->CreateMatchTable ( LoadedPal );
  198.                     Grafix.AdjustImageSize ( Texture, 256, 256 );
  199.                     Grafix.ConvertImage ( Texture, MatchTable );
  200.                   } // End if
  201.                 else if (FileType==TEXTURE_FLIC)
  202.                   {
  203.                     Flic->Convert ( Pal );  
  204.                     Flic->SetFrame ( 0 );
  205.                     Texture->CreateBaseImage ();
  206.                     Texture->CreateFrames ( Flic->GetNumFrames() );
  207.                     Grafix.SetScaleFactor ( 256, Flic->GetImageWidth(),
  208.                                             256, Flic->GetImageHeight() );                                            
  209.                     for (i=0;i<Flic->GetNumFrames();i++)
  210.                       {
  211.                         Flic->PlayFrame ( TRUE );
  212.                         Grafix.ScaleImage  ( Flic->GetImage (), 0, 0,                        
  213.                                              Flic->GetImageWidth(), Flic->GetImageHeight(),
  214.                                              Texture, 0, 0 );
  215.                         Texture->RecordFrame ( i, Texture, 0, 0 );
  216.                       } // End for
  217.                     Grafix.SetScaleFactor ( 1, 1, 1, 1 );
  218.                   } // End else if  
  219.               } // End if  
  220.           } // End if  
  221.       } // End if
  222.  
  223.     if (Flic!=NULL)
  224.       {
  225.         delete Flic;
  226.         LoadedPal = NULL;
  227.       } // End if  
  228.  
  229.     if (LoadedPal!=NULL)  
  230.       delete LoadedPal;
  231.  
  232.     if (MatchTable!=NULL)        
  233.       delete MatchTable;
  234.  
  235.     return Result;  
  236.   } // End of LoadTaexture for G3DMATERIALLIB  
  237.